package com.situ.object.service.impl;

import com.situ.object.common.Page;
import com.situ.object.dao.StudentDao;
import com.situ.object.dao.impl.StudentDaoImpl;
import com.situ.object.entity.Student;
import com.situ.object.service.StudentService;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    //合成
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public Page getForPage(Integer page, Integer limit, Student student) {
        Page pageInfo = new Page();

        //将sql作为参数传递给dao
        //where 1=1    用于解决条件为空的问题
        String sql = "select s.*,d.teacherName as teacher_name from student s " + " left join teacher d on s.teacherId=d.teacherId  where status = 0 ";
        //多个条件 动态拼接 where
        //参数
        List<Object> params = new ArrayList<>();

            if (student.getStudentName()!=null&& !"".equals(student.getStudentName())){
                sql += " and s.studentName like ? ";
                params.add("%"+ student.getStudentName() + "%");
            }
            if (student.getTeacherId()!=null){
                sql += " and s.teacherId = ? ";
                params.add(student.getTeacherId());
            }



        Long count = studentDao.selectCount(sql,params);
        List list = studentDao.selectList(page,limit,sql,params);

        //将字段总数和
        pageInfo.setCount(count);
        pageInfo.setList(list);
        System.out.println("student:"+pageInfo);
        return pageInfo;
    }

    @Override
    public int add(Student student) {
        return studentDao.add(student);
    }

    @Override
    public int edit(Student student) throws Exception {
        return studentDao.update(student);
    }

    @Override
    public int delete(Integer id) {
        return studentDao.delete(id);
    }

    @Override
    public List<Student> selectAll() {
        return studentDao.selectAll();
    }
}
